package chap04.Reverse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by root on 2017/6/30.
 */
class StackC{
    private int maxSize;
    private char[] stackArray;
    private int top;

    public StackC(int maxSize) {
        this.maxSize = maxSize;
        stackArray = new char[maxSize];
        top = -1;
    }

    public void push(char j) {
        stackArray[++top] = j;
    }

    public char pop() {
        return stackArray[top--];
    }

    public long peek() {
        return stackArray[top];
    }

    public boolean isEmpty() {
        return (top == -1);
    }

    public boolean isFull() {
        return (top == maxSize-1);
    }

    public  int size() {
        return top+1;
    }

    public char peekN(int n) {
        return stackArray[n];
    }

    public void displayStack(String s) {
        System.out.print(s);
        System.out.print("Stack (bottom-->top): ");
        for (int i=0; i<size(); i++) {
            System.out.print(peekN(i));
            System.out.print(' ');
        }
        System.out.println();
    }
}
class Reverser{
    private String input;
    private String output;

    public Reverser(String input) {
        this.input = input;
    }

    public String doRev() {
        int stackSize = input.length();
        StackC stackX = new StackC(stackSize);

        for (int i=0; i<stackSize; i++) {
            char ch = input.charAt(i);
            stackX.push(ch);
        }

        output = "";

        while (!stackX.isEmpty()) {
            char ch = stackX.pop();
                    output+=ch;
        }
        return output;
    }
}
public class ReverserApp {
    public static void main(String[] args) throws IOException{
        String input, output;
        while (true) {
            System.out.print("Enter a string");
            System.out.flush();
            input = getString();
            if (input.equals("")) {
                break;
            }
            Reverser reverser = new Reverser(input);
            output = reverser.doRev();
            System.out.println("Reversed: " + output);
        }
    }
    public static String getString() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
    }
}
